package com.splant.android.smart.SystemDefinedLevel.WebView;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

/**
 * Created by JimOPC on 15-3-28.
 */
public class MyWebChromeClient extends WebChromeClient {
    private Context curContext;
    public MyWebChromeClient(Context context)
    {
        curContext=context;
    }

    // 处理Alert事件
    @Override
    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {

// 构建一个Builder来显示网页中的alert对话框

        AlertDialog.Builder builder = new AlertDialog.Builder(curContext);
        builder.setTitle("提示：");
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result.confirm();
            }

        });
        builder.setCancelable(false);
        builder.create();
        builder.show();
        return true;
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {

        super.onReceivedTitle(view, title);

    }

// 处理Confirm事件

    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {

        AlertDialog.Builder builder = new AlertDialog.Builder(curContext);
        builder.setTitle("删除确认");
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                result.confirm();
            }

        });

        builder.setNeutralButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result.cancel();

            }

        });
        builder.setCancelable(false);
        builder.create();
        builder.show();
        return true;

    }
    // 处理提示事件
    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,

                              JsPromptResult result) {
// 看看默认的效果
        return super.onJsPrompt(view, url, message, defaultValue, result);
    }
}
